<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>入库API调试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }

        .debug-section {
            margin: 20px 0;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }

        .success {
            background-color: #d4edda;
            border-color: #c3e6cb;
        }

        .error {
            background-color: #f8d7da;
            border-color: #f5c6cb;
        }

        .info {
            background-color: #d1ecf1;
            border-color: #bee5eb;
        }

        button {
            padding: 10px 20px;
            margin: 10px;
            background: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        button:hover {
            background: #0056b3;
        }

        pre {
            white-space: pre-wrap;
            word-wrap: break-word;
            background: #f8f9fa;
            padding: 10px;
            border-radius: 4px;
        }

        .loading {
            color: #007bff;
        }
    </style>
</head>

<body>
    <h1>入库API调试页面</h1>

    <div class="debug-section info">
        <h3>测试说明</h3>
        <p>此页面用于调试入库API调用，检查数据是否正确返回。</p>
    </div>

    <div class="debug-section">
        <h3>API测试</h3>
        <button onclick="testApi()">测试入库API</button>
        <button onclick="testWithParams()">测试带参数API</button>
        <div id="apiResult"></div>
    </div>

    <div class="debug-section">
        <h3>网络状态</h3>
        <button onclick="checkNetwork()">检查网络连接</button>
        <div id="networkResult"></div>
    </div>

    <script>
        async function testApi() {
            const resultDiv = document.getElementById('apiResult');
            resultDiv.innerHTML = '<div class="loading">正在测试API...</div>';

            try {
                console.log('开始测试入库API...');

                const response = await fetch('http://localhost:5000/api/Inbound/list', {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json',
                    }
                });

                console.log('响应状态:', response.status);
                console.log('响应头:', response.headers);

                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }

                const data = await response.json();
                console.log('API响应数据:', data);

                if (data.code === 200) {
                    resultDiv.innerHTML = `
                        <div class="success">
                            <h4>✅ API调用成功</h4>
                            <p><strong>状态码:</strong> ${data.code}</p>
                            <p><strong>消息:</strong> ${data.message}</p>
                            <p><strong>总条数:</strong> ${data.data.totalCount}</p>
                            <p><strong>当前页数据条数:</strong> ${data.data.pageData.length}</p>
                            <h5>数据预览:</h5>
                            <pre>${JSON.stringify(data.data.pageData.slice(0, 3), null, 2)}</pre>
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <h4>❌ API返回错误</h4>
                            <p><strong>状态码:</strong> ${data.code}</p>
                            <p><strong>消息:</strong> ${data.message}</p>
                        </div>
                    `;
                }

            } catch (error) {
                console.error('API测试失败:', error);
                resultDiv.innerHTML = `
                    <div class="error">
                        <h4>❌ API调用失败</h4>
                        <p><strong>错误:</strong> ${error.message}</p>
                        <p>请检查后端服务是否正在运行在 http://localhost:5000</p>
                    </div>
                `;
            }
        }

        async function testWithParams() {
            const resultDiv = document.getElementById('apiResult');
            resultDiv.innerHTML = '<div class="loading">正在测试带参数的API...</div>';

            try {
                console.log('开始测试带参数的入库API...');

                const response = await fetch('http://localhost:5000/api/Inbound/list?pageIndex=1&pageSize=5', {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json',
                    }
                });

                console.log('响应状态:', response.status);

                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }

                const data = await response.json();
                console.log('API响应数据:', data);

                if (data.code === 200) {
                    resultDiv.innerHTML = `
                        <div class="success">
                            <h4>✅ 带参数API调用成功</h4>
                            <p><strong>状态码:</strong> ${data.code}</p>
                            <p><strong>消息:</strong> ${data.message}</p>
                            <p><strong>总条数:</strong> ${data.data.totalCount}</p>
                            <p><strong>当前页数据条数:</strong> ${data.data.pageData.length}</p>
                            <p><strong>分页信息:</strong> 第${data.data.pageIndex}页，每页${data.data.pageSize}条</p>
                            <h5>数据预览:</h5>
                            <pre>${JSON.stringify(data.data.pageData, null, 2)}</pre>
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <h4>❌ 带参数API返回错误</h4>
                            <p><strong>状态码:</strong> ${data.code}</p>
                            <p><strong>消息:</strong> ${data.message}</p>
                        </div>
                    `;
                }

            } catch (error) {
                console.error('带参数API测试失败:', error);
                resultDiv.innerHTML = `
                    <div class="error">
                        <h4>❌ 带参数API调用失败</h4>
                        <p><strong>错误:</strong> ${error.message}</p>
                    </div>
                `;
            }
        }

        async function checkNetwork() {
            const resultDiv = document.getElementById('networkResult');
            resultDiv.innerHTML = '<div class="loading">正在检查网络连接...</div>';

            try {
                // 测试基本连接
                const response = await fetch('http://localhost:5000/api/Inbound/list', {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json',
                    }
                });

                resultDiv.innerHTML = `
                    <div class="success">
                        <h4>✅ 网络连接正常</h4>
                        <p><strong>状态码:</strong> ${response.status}</p>
                        <p><strong>状态文本:</strong> ${response.statusText}</p>
                        <p><strong>CORS:</strong> ${response.headers.get('Access-Control-Allow-Origin') || '未设置'}</p>
                    </div>
                `;

            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <h4>❌ 网络连接失败</h4>
                        <p><strong>错误:</strong> ${error.message}</p>
                        <p>可能的原因:</p>
                        <ul>
                            <li>后端服务未启动</li>
                            <li>CORS配置问题</li>
                            <li>网络连接问题</li>
                        </ul>
                    </div>
                `;
            }
        }

        // 页面加载时自动检查网络
        window.onload = function () {
            checkNetwork();
        };
    </script>
</body>

</html>